﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.Administration.Entities;
using HerGoS.ControlsUI;
using HerGoS.Administration.Engine;
using HerGoS.WebControlExtend;

namespace HerGoS.WebUI.Administracion.Areas
{
    public partial class Areas_Listado : PageBase
    {
        #region Propiedades
        public List<Area> Areas
        {
            get
            {
               if (ViewState["areas"] != null)
                    return (List<Area>)ViewState["areas"];
                return null;
            }
            set
            {
                ViewState["areas"] = value;
            }

        }
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
           if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridAreas(false);
            }

        }

         void Initialize()
        {
            this.GridAreas.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());
        }

         private void LoadGridAreas(bool cache)
         {
             if (!cache)
                 Areas = GestorArea.Consultar();
             this.GridAreas.DataSource = Areas;
             this.GridAreas.DataBind();
         }

         protected void btnRefresh_Click(object sender, EventArgs e)
         {
             LoadGridAreas(false);
         }



         protected void btnBorrar_Click(object sender, EventArgs e)
         {
             ImageButton btn = (ImageButton)sender;
             int index = Convert.ToInt32(btn.CommandArgument);
             int id = Convert.ToInt32(GridAreas.DataKeys[index].Value);

             Area area = Areas.Find(c => c.OID == id);
             Areas.Remove(area);

             GestorArea.Eliminar(area);
             LoadGridAreas(true);

         }

         protected void GridAreas_RowDataBound(object sender, GridViewRowEventArgs e)
         {
             if (e.Row.RowType == DataControlRowType.DataRow)
             {
                 ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                 if (Editar != null)
                 {
                     Editar.Settings(new ImageButtonExtEdit());
                     int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                     Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                 }

                 ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                 if (Borrar != null)
                 {
                     Borrar.Settings(new ImageButtonExtDelete());
                 }
             }
         }

         protected void GridAreas_PageIndexChanging(Object sender, GridViewPageEventArgs e)
         {
             this.GridAreas.PageIndex = e.NewPageIndex;
             LoadGridAreas(true);
         } 





    }
}